<?php

defined('SYSPATH') or die('Access error!');

class Model_Category extends Model
{

    private $menu_option = array();
    private $all_children = array();

    public function get_category_list($where = '')
    {
        $where = !empty($where) ? ' AND ' . $where : '';
        $where .= ' ORDER BY position,id';
        $category_list = $this->get_results($where);
        return Arr::validate_arr($category_list) > 0 ? $category_list : array();
    }

    public function get_category_option($where = '', $option = '', $selectid = 0)
    {
        $category_list = $this->get_category_list($where);
        Tree::instance()->setTree($category_list, 'id', 'parent_id', 'name');
        if ($option == '')
        {
            $option = "<option value=\$id \$selected>\$spacer\$name</option>";
        }
        return Tree::instance()->get_tree(0, $option, $selectid);
    }

    public function get_menu_option($id, $selectid = 0)
    {
        //生成分类树
        $category_list = $this->get_category_list();
        $tree = Tree::instance();
        $tree->setTree($category_list, 'id', 'parent_id', 'name');
        $this->create_menu_option($id, $selectid, $tree);
        
        $menu_option = array_reverse($this->menu_option);
        $option_str = '';
        if (Arr::validate_arr($menu_option))
        {
            foreach ($menu_option as $key => $value)
            {
                $option_str .= $value;
            }
        }
        return $option_str;
    }

    public function create_menu_option($id, $selectid = 0, $tree)
    {
        $str = "<select name='parent_id[]' class='parent_id' onchange='change_category(this)'><option value='0'>==不选择==</option>";
        $parent_id_arr = $tree->get_parent($id);
        if (Arr::validate_arr($parent_id_arr))
        {
            foreach ($parent_id_arr as $key => $value)
            {
                $str .= "<option value=" . $value['id'];
                if ($value['id'] == $selectid)
                {
                    $str.=" selected='selected'";
                }
                $str .= ">" . $value['name'] . "</option>";
            }
            $str .="</select>";
            $this->menu_option[] = $str;
            if (isset($parent_id_arr[$selectid]['parentid']) && $parent_id_arr[$selectid]['parentid'] != 0)
            {
                $this->create_menu_option($parent_id_arr[$selectid]['id'], $parent_id_arr[$selectid]['parentid'], $tree);
            }
        }
    }

    public function get_category_option2($id, $option = '', $selectid = 0)
    {
        $where = 'parent_id=' . $id;
        $category_list = $this->get_category_list($where);
        Tree::instance()->setTree($category_list, 'id', 'parent_id', 'name');
        if ($option == '')
        {
            $option = "<option value = \$id \$selected>\$spacer\$name</option>";
        }

        return Tree::instance()->get_tree($id, $option, $selectid);
    }

    public function get_sub_category_option($parent_id, $selectid = 0)
    {
        $category_list = $this->get_category_list();
        Tree::instance()->setTree($category_list, 'id', 'parent_id', 'name');
        return Tree::instance()->get_tree($parent_id, "<option value = \$id \$selected>\$spacer\$name</option>", $selectid);
    }

    public function get_parent($id = 0)
    {
        $where = '';
        if ($id != 0)
        {
            $where = 'id=' . $id;
        }
        $category_list = $this->get_category_list($where);
        Tree::instance()->setTree($category_list, 'id', 'parent_id', 'name');
    }
    
    

    /**
     *  根据分类的id，获取seo相关的数据，如果为空即为分类名称
     * @param type $id
     * @return type
     */
    public function get_category_seo($id, $first_id = 0)
    {
        if (empty($id))
        {
            $id = $first_id;
        }
        $title_path = $this->get_category_position($id, $first_id);
        $category_seo_temp = $this->get_row(array('id' => $id), 'name,title,keywords,description');
        $category_seo = array();
        if (Arr::validate_arr($category_seo_temp))
        {
            if (Arr::validate_arr($title_path))
            {
                $title_arr = Arr::pluck($title_path, 'name');
            }
            $title_arr = array_reverse($title_arr);
            $category_seo['title'] = Arr::validate_arr($title_arr) ? $title_arr : $category_seo_temp['name'];
            $category_seo['keywords'] = !empty($category_seo_temp['keywords']) ? $category_seo_temp['keywords'] : $category_seo_temp['name'];
            $category_seo['description'] = !empty($category_seo_temp['description']) ? $category_seo_temp['description'] : $category_seo_temp['name'];
        }
        return $category_seo;
    }

    /**
     *  获取当前路径的树
     * @param type $id
     * @param type $first_id
     * @return type
     */
    public function get_category_position($id, $first_id = '')
    {
        if (empty($id))
        {
            $id = $first_id;
        }
        $category_list = $this->get_category_list($first_id);
        Tree::instance()->setTree($category_list, 'id', 'parent_id', 'name');
        $position_arr = Tree::instance()->get_pos($id);
        return $position_arr;
    }

    /**
     * 取得某分类的所有子孙分类id
     */
    public function get_all_children($id)
    {
        $this->all_children[] = $id;
        $this->_get_all_children($id);
        return $this->all_children;
    }

    /**
     *  递归调用所有子分类（用于小数据）
     * @param type $id
     */
    private function _get_all_children($id)
    {
        $childs = $this->get_category_list('parent_id=' . $id);
        foreach ($childs as $child)
        {
            $this->all_children[] = intval($child['id']);
            $this->_get_all_children($child['id']);
        }
    }

}